package com.cskaoyan.service.wx.ddp;

import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.module.ddp.bo.CouponMyListParamsBo;
import com.cskaoyan.module.ddp.vo.FootPrintVo;
import com.cskaoyan.module.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * FootPrintServiceImpl
 *
 * @param
 * @return
 * @2022/4/11
 **/
@Service
public class FootPrintServiceImpl implements FootPrintService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketFootprintMapper footprintMapper;

    @Override
    public FootPrintVo list(CouponMyListParamsBo params) {
        FootPrintVo result = new FootPrintVo();

        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer id=user.getId();

        MarketFootprintExample example=new MarketFootprintExample();
        example.setOrderByClause("update_time desc");
        MarketFootprintExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andUserIdEqualTo(id);

        PageHelper.startPage(params.getPage(),params.getLimit());
        List<MarketFootprint> footprints = footprintMapper.selectByExample(example);
        List<FootPrintVo.ListDTO> list=new ArrayList<>();
        for (MarketFootprint footprint : footprints) {
            MarketGoods goods = marketGoodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            FootPrintVo.ListDTO listDTO = new FootPrintVo.ListDTO();
            listDTO.setGoodsId(footprint.getGoodsId());
            listDTO.setId(footprint.getId());
            listDTO.setName(goods.getName());
            listDTO.setPicUrl(goods.getPicUrl());
            listDTO.setRetailPrice(goods.getRetailPrice());
            listDTO.setBrief(goods.getBrief());
            listDTO.setAddTime(goods.getAddTime());
            list.add(listDTO);
        }

        PageInfo<MarketFootprint> pageInfo=new PageInfo<>(footprints);
        int pages = pageInfo.getPages();
//        Integer total = ((Integer) pageInfo.getTotal());
        int pageNum = pageInfo.getPageNum();
        result.setList(list);
        result.setPage(pageNum);
        result.setPages(pages);
        result.setTotal(pageInfo.getTotal());
        result.setLimit(pageInfo.getSize());
        return result;
    }

    @Override
    public int delete(Integer param) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer id=user.getId();

        MarketFootprintExample example=new MarketFootprintExample();

        MarketFootprintExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andUserIdEqualTo(id);
        criteria.andIdEqualTo(param);
        List<MarketFootprint> footprints = footprintMapper.selectByExample(example);
        MarketFootprint marketFootprint = footprints.get(0);
        marketFootprint.setUpdateTime(new Date());
        marketFootprint.setDeleted(true);
        footprintMapper.updateByPrimaryKey(marketFootprint);
        return 1;
    }
}
